package cn.qlin.mvc.orm.util;

import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.qlin.mvc.orm.bean.UpdateTable;
import cn.qlin.mvc.orm.bean.Where;

public class CreateSql {
	public static String creatQuery(List<String> cols,String tablename,Where where) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		for(int i=0;i<cols.size();i++){
			sql.append(cols.get(i)+",");
		}
		sql.delete(sql.length()-1, sql.length());
		sql.append(" FROM "+tablename);
		if(where.get()!=null){
			sql.append(" WHERE "+ where.get());
		}
		return sql.toString();
	}
	public String createDelete(String[] formwhere,String tablename){
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM ");
		sql.append(tablename+" WHERE ");
		
		sql.append("`"+formwhere[0]+"`="+formwhere[1]);
		System.out.println(sql);
		return sql.toString();
	}
	public String createUpdate(UpdateTable cols,String tablename){
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append("`"+tablename+"` SET ");
		Map<String, String> formcols = cols.getCols();
		Map<String, String> formwhere = cols.getWhere();
		Set<String> kset =cols.getCols().keySet();
		
		for(String ks:kset){
			sql.append("`"+ks+"`="+formcols.get(ks)+",");
		}
		sql.delete(sql.length()-1, sql.length());
		sql.append(" WHERE ");
		Set<String> where =cols.getWhere().keySet();
		
		for(String ks:where){
			sql.append("`"+ks+"`="+formwhere.get(ks)+" AND ");
		}
		sql.delete(sql.length()-5, sql.length());
		System.out.println(sql);
		return sql.toString();
	}
	public String createInsert(Map<String, String> cols,String tablename){
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO ");
		sql.append("`"+tablename+"` ");
		Set<String> kset =cols.keySet();
		StringBuffer key = new StringBuffer("(");
		StringBuffer values = new StringBuffer("(");
		
		for(String ks:kset){
			key.append("`"+ks+"`,");
			values.append(cols.get(ks)+",");			
		}
		key.delete(key.length()-1, key.length());
		values.delete(values.length()-1, values.length());
		key.append(")");
		values.append(")");	
		sql.append(key);
		sql.append("VALUES");
		sql.append(values);
		System.out.println(sql);
		return sql.toString();
	}
}